home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / MMATH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  3.9 KB  |  145 lines

  1. /* $Id: mmath.c,v 3.0 1998/01/31 20:59:27 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: mmath.c,v $
  26.  * Revision 3.0  1998/01/31 20:59:27  brianp
  27.  * initial rev
  28.  *
  29.  */
  30.  
  31.  
  32. #ifdef PC_HEADER
  33. #include "all.h"
  34. #else
  35. #include "GL/gl.h"
  36. #include "mmath.h"
  37. #endif
  38.  
  39.  
  40.  
  41. /*
  42.  * A High Speed, Low Precision Square Root
  43.  * by Paul Lalonde and Robert Dawson
  44.  * from "Graphics Gems", Academic Press, 1990
  45.  */
  46.  
  47. /*
  48.  * SPARC implementation of a fast square root by table 
  49.  * lookup.
  50.  * SPARC floating point format is as follows:
  51.  *
  52.  * BIT 31     30     23     22     0
  53.  *     sign    exponent    mantissa
  54.  */
  55. static short sqrttab[0x100];    /* declare table of square roots */
  56.  
  57. static void init_sqrt(void)
  58. {
  59. #ifdef FAST_MATH
  60.    unsigned short i;
  61.    float f;
  62.    unsigned int *fi = (unsigned int *)&f;
  63.                                 /* to access the bits of a float in  */
  64.                                 /* C quickly we must misuse pointers */
  65.  
  66.    for(i=0; i<= 0x7f; i++) {
  67.       *fi = 0;
  68.  
  69.       /*
  70.        * Build a float with the bit pattern i as mantissa
  71.        * and an exponent of 0, stored as 127
  72.        */
  73.  
  74.       *fi = (i << 16) | (127 << 23);
  75.       f = sqrt(f);
  76.  
  77.       /*
  78.        * Take the square root then strip the first 7 bits of
  79.        * the mantissa into the table
  80.        */
  81.  
  82.       sqrttab[i] = (*fi & 0x7fffff) >> 16;
  83.  
  84.       /*
  85.        * Repeat the process, this time with an exponent of
  86.        * 1, stored as 128
  87.        */
  88.  
  89.       *fi = 0;
  90.       *fi = (i << 16) | (128 << 23);
  91.       f = sqrt(f);
  92.       sqrttab[i+0x80] = (*fi & 0x7fffff) >> 16;
  93.    }
  94. #endif /*FAST_MATH*/
  95. }
  96.  
  97.  
  98. float gl_sqrt( float x )
  99. {
  100. #ifdef FAST_MATH
  101.    unsigned int *num = (unsigned int *)&x;
  102.                                 /* to access the bits of a float in C
  103.                                  * we must misuse pointers */
  104.                                                         
  105.    short e;                     /* the exponent */
  106.    if (x == 0.0F) return 0.0F;  /* check for square root of 0 */
  107.    e = (*num >> 23) - 127;      /* get the exponent - on a SPARC the */
  108.                                 /* exponent is stored with 127 added */
  109.    *num &= 0x7fffff;            /* leave only the mantissa */
  110.    if (e & 0x01) *num |= 0x800000;
  111.                                 /* the exponent is odd so we have to */
  112.                                 /* look it up in the second half of  */
  113.                                 /* the lookup table, so we set the   */
  114.                                 /* high bit                                */
  115.    e >>= 1;                     /* divide the exponent by two */
  116.                                 /* note that in C the shift */
  117.                                 /* operators are sign preserving */
  118.                                 /* for signed operands */
  119.    /* Do the table lookup, based on the quaternary mantissa,
  120.     * then reconstruct the result back into a float
  121.     */
  122.    *num = ((sqrttab[*num >> 16]) << 16) | ((e + 127) << 23);
  123.    return x;
  124. #else
  125.    return sqrt(x);
  126. #endif
  127. }
  128.  
  129.  
  130.  
  131. /*
  132.  * Initialize tables, etc for fast math functions.
  133.  */
  134. void gl_init_math(void)
  135. {
  136.    static GLboolean initialized = GL_FALSE;
  137.  
  138.    if (!initialized) {
  139.       init_sqrt();
  140.  
  141.  
  142.       initialized = GL_TRUE;
  143.    }
  144. }
  145.